import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: qian
 * Date: 2023-02-08
 * Time: 16:59
 */
public class test5 {
    //使用callable，创建线程计算1+2+。。。+1000的值
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Callable<Integer> callable=new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int sum=0;
                for(int i=1;i<=1000;i++){
                    sum+=i;
                }
                return sum;
            }
        };

        //Thread不能直接传callable，需要借助FutureTask
        //FutureTask存在的意义就是让我们能够获取到结果
        FutureTask<Integer> futureTask=new FutureTask<>(callable);
        Thread t=new Thread(futureTask);
        t.start();

        //get()方法会阻塞，一直到callable计算完毕，get方法才会返回
        System.out.println(futureTask.get());

    }
}
